package com.junsi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junsi.bean.Approve;
import com.junsi.bean.Distribution;
import com.junsi.bean.Staff;
import com.junsi.mapper.StaffMapper;
import com.junsi.service.DistributionSerivce;
import com.junsi.service.StaffSerivce;
import com.junsi.util.InteractiveUtil;
import com.junsi.util.PageUtil;

/**
 * 员工Serivce接口实现类
 * @author wkp
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffSerivceImpl implements StaffSerivce {

	@Autowired
	private StaffMapper staffMapper;
	
	@Autowired
	private DistributionSerivce distributionSerivce;
	
	@Override
	public Staff findById(Integer id) {
		Staff staff = staffMapper.findById(id);
		return staff;
	}

	@Override
	public List<Staff> findByAll() {
		
		return staffMapper.findByAll();
	}

	@Override
	public boolean save(Staff staff) {
		int i = staffMapper.save(staff);
		if(i == 1){
			Distribution distribution = new Distribution();
			distribution.setDistribution_roleId(5);
			distribution.setDistribution_staffId(staff.getStaff_id());
			boolean reuslt = distributionSerivce.save(distribution);
			if(reuslt){
				return true;
			}else{
				staffMapper.delete(distribution.getDistribution_id());
			}
			
		}else{
			
		}
		return false;
	}

	@Override
	public boolean update(Staff staff) {
		int i = staffMapper.update(staff);
		if(i == 1){
			return true;
		}
		return false;
	}

	@Override
	public boolean delete(Integer id) {
		int i = staffMapper.delete(id);
		if(i == 1){
			return true;
		}
		return false;
	}


	@Override
	public List<Staff> findByDepartment(Integer id, Integer endIndex, Integer startIndex) {
		
		if(endIndex == null){
			return null;
		}
		if(startIndex == null){
			return null;
		}
		
		return staffMapper.findByDepartment(id, endIndex, startIndex);
	}

	@Override
	public Integer findByDepartmentNum(Integer id) {
		
		return staffMapper.findByDepartmentNum(id);
	}

	@Override
	public Staff findByUserName(String staff_userName) {
		
		return staffMapper.findByUserName(staff_userName);
	}

	@Override
	public List<Staff> findBySuperior(Integer staffId) {
		
		return staffMapper.findBySuperior(staffId);
	}

	@Override
	public List<Staff> findByDepartmentId(Integer departmentId) {
		
		return staffMapper.findByDepartmentId(departmentId);
	}

	@Override
	public String findByName(String staff_id) {
		
		if(staff_id != null){
			List<String> list = staffMapper.findByName(staff_id);
			if(list.size() != 0){
				String staffName = "";
				for (String string : list) {
					staffName += ""+string+",";
				}
				staffName = staffName.substring(0, staffName.length()-1);
				return staffName;
			}
		}
		
		return null;
	}

	@Override
	public boolean deletes(String id) {
		String[] arr = id.split(",");
		
		int i = staffMapper.deletes(arr);
		if(i != 0){
			return true;
		}
		return false;
	}

	@Override
	public List<Staff> findByDepId(Integer id) {
		
		return staffMapper.findByDepId(id);
	}

	@Override
	public List<Staff> findSelectWhere(String selectWhere) {
		Staff staff = new Staff();
		selectWhere ="%"+selectWhere+"%"; 
		staff.setStaff_name(selectWhere);
		return staffMapper.findSelectWhere(staff);
	}

	@Override
	public List<Staff> findByOneName(String name) {
		
		return staffMapper.findByOneName(name);
	}

	@Override
	public List<Staff> findAllPage(PageUtil pageUtil) {
		List<Staff> page=staffMapper.findAllPage(pageUtil.getEndIndex(), pageUtil.getBeginIndex());
		return page;
	}

	@Override
	public int findAllCount() {
	  int count=staffMapper.findAllCount();
	  return count;
	}

	@Override
	public int findAllCountCondition(String condition) {
		 int count=staffMapper.findAllCountCondition(condition);
		  return count;
	}

	@Override
	public List<Staff> findAllPageCondition(PageUtil pageUtil, String condition) {
		List<Staff> page=staffMapper.findAllPageCondition(pageUtil.getEndIndex(),condition, pageUtil.getBeginIndex());
		return page;
	}

}
